

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  
    <meta name="description" content="@[toc] 参考资料 视频资料 运行环境  windows10 JDK 8 IDEA 专业版 SpringBoot 2.5.6  一、SpringBoot 监听机制 SpringBoot的监听机制，其实是对Java提供的事件监听机制的封装 Java中的事件监听机制定义了以下几个角色：  事件：Event ，继承 java.util.EventObject类的对象 事件源：Source，任意对象">
<meta property="og:type" content="article">
<meta property="og:title" content="SpringBoot 笔记4 SpringBoot原理分析 _ 四个监听机制 _ 通过断点调试进行原理分析 _ Admin监控机制的运用 _ SpringBoot部署模式切换jar与war">
<meta property="og:url" content="http://example.com/2022/01/12/SpringBoot%20%E7%AC%94%E8%AE%B04%20SpringBoot%E5%8E%9F%E7%90%86%E5%88%86%E6%9E%90%20_%20%E5%9B%9B%E4%B8%AA%E7%9B%91%E5%90%AC%E6%9C%BA%E5%88%B6%20_%20%E9%80%9A%E8%BF%87%E6%96%AD%E7%82%B9%E8%B0%83%E8%AF%95%E8%BF%9B%E8%A1%8C%E5%8E%9F%E7%90%86%E5%88%86%E6%9E%90%20_%20Admin%E7%9B%91%E6%8E%A7%E6%9C%BA%E5%88%B6%E7%9A%84%E8%BF%90%E7%94%A8%20_%20SpringBoot%E9%83%A8%E7%BD%B2%E6%A8%A1%E5%BC%8F%E5%88%87%E6%8D%A2jar%E4%B8%8Ewar/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="@[toc] 参考资料 视频资料 运行环境  windows10 JDK 8 IDEA 专业版 SpringBoot 2.5.6  一、SpringBoot 监听机制 SpringBoot的监听机制，其实是对Java提供的事件监听机制的封装 Java中的事件监听机制定义了以下几个角色：  事件：Event ，继承 java.util.EventObject类的对象 事件源：Source，任意对象">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/0c44d497c8eb454787dcec8e04640122.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/095b576b05e74cd79cd83978c0374ae0.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/925f4bcd374143a2b54f2cb06fd7e160.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/ded6ad69c8eb465d8a4db7f95bbf7a70.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/91c2da7e9b6a43ddb0a4faf27e8b841a.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/78ef4ddfc1f14a0dad820d9297e97679.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/69c91009159e498bb99619a81b884519.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/bfbcbee7b41c4edb8ab32bdbbaf8ea22.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/58f97577d9984fd9be4fa29e6af0748c.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/a2d70fcb2ed94e73bfad45f4a06c3959.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/01526ec43215484d8142f73f61cccc10.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/43c4be8cf2e94fd19d0382f32bb0312f.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/d44aef738c2041fa921993fc6e479c70.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/363b1ff79d224f6bbbb58b462a7202de.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/b97d5c0f965f4aec8b76019868610177.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/ec6af53be8e7473c813d72b24f382ca5.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/e157c9cf5aac4ce383c39f9e6d6d3005.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/1fee780e5783468aa18cf4f2fb41c47e.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/1486f833a53c4a62a36aa1402258c640.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/7092eeb4f1444a7ab0f5b168b1a33e98.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/a48a9ec11df94ea6b39ed5201736699d.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/c78ddbf9c0674e42b6abb5983ab5546a.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/85f49902b31841578f02629e3f004216.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/9aee01a8bb0843df8d328069f5a7ae6b.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/6da37d6756604ac4a1d8ea58b6883775.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/ad4fa88b0f6c4a97bd51ede61b26475c.png#pic_Center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/54f5fca77b6442fb8de578fa94a0e765.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/e5c02e81be144e44a0ba3cb0da59ca0d.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/c693d37fb9d3453686432e866bfa367e.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/359a12610f4144eda125d364e716b54e.png#pic_center">
<meta property="article:published_time" content="2022-01-12T02:59:34.000Z">
<meta property="article:modified_time" content="2022-08-22T15:48:54.224Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="SpringBoot">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/0c44d497c8eb454787dcec8e04640122.png#pic_center">
  
  
  
  <title>SpringBoot 笔记4 SpringBoot原理分析 _ 四个监听机制 _ 通过断点调试进行原理分析 _ Admin监控机制的运用 _ SpringBoot部署模式切换jar与war - Hexo</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>尤 Ni&#39;s Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="SpringBoot 笔记4 SpringBoot原理分析 _ 四个监听机制 _ 通过断点调试进行原理分析 _ Admin监控机制的运用 _ SpringBoot部署模式切换jar与war"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        John Doe
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-01-12 10:59" pubdate>
          2022年1月12日 上午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          13k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          113 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">SpringBoot 笔记4 SpringBoot原理分析 _ 四个监听机制 _ 通过断点调试进行原理分析 _ Admin监控机制的运用 _ SpringBoot部署模式切换jar与war</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：1 小时前
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <p>@[toc]</p>
<h1 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h1><hr>
<p><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1Lq4y1J77x?p=34&spm_id_from=pageDriver">视频资料</a></p>
<h1 id="运行环境"><a href="#运行环境" class="headerlink" title="运行环境"></a>运行环境</h1><hr>
<ul>
<li>windows10</li>
<li>JDK 8</li>
<li>IDEA 专业版</li>
<li>SpringBoot 2.5.6</li>
</ul>
<h1 id="一、SpringBoot-监听机制"><a href="#一、SpringBoot-监听机制" class="headerlink" title="一、SpringBoot 监听机制"></a>一、SpringBoot 监听机制</h1><hr>
<p>SpringBoot的监听机制，其实是对Java提供的事件监听机制的封装</p>
<p>Java中的事件监听机制定义了以下几个角色：</p>
<ul>
<li>事件：<code>Event </code>，继承 <code>java.util.EventObject</code>类的对象</li>
<li>事件源：<code>Source</code>，任意对象 <code>Object</code></li>
<li>监听器：<code>Listener</code>，实现 <code>java.util.EventListener</code>接口的对象</li>
</ul>
<p>在 SpringBoot项目启动时，会对几个监听器进行回调，我们可以实现这些监听器接口，在项目启动时完成一些操作</p>
<ul>
<li>ApplicationContextInitializer</li>
<li>SpringApplicationRunListener</li>
<li>CommandLineRunner</li>
<li>ApplicationRunner</li>
</ul>
<p>测试的项目结构：</p>
<p>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/95abbd1e31db423d87bea6c82bf1e491.png#pic_center">https://img-blog.csdnimg.cn/95abbd1e31db423d87bea6c82bf1e491.png#pic_center</a> &#x3D;50%x)</p>
<p>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/530f5ffef38c43ae9551c6596291bab1.png#pic_center">https://img-blog.csdnimg.cn/530f5ffef38c43ae9551c6596291bab1.png#pic_center</a> &#x3D;80%x)</p>
<p><code>spring.factories</code> 用于指定springboot容器的监听类</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs xml">org.springframework.context.ApplicationContextInitializer=com.uni.listener.MyApplicationContextInitializer<br>org.springframework.boot.SpringApplicationRunListener=com.uni.listener.MySpringApplicationRunListener<br></code></pre></td></tr></table></figure>



<p><code>MyApplicationContextInitializer.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.uni.listener;<br><br><span class="hljs-keyword">import</span> org.springframework.context.ApplicationContextInitializer;<br><span class="hljs-keyword">import</span> org.springframework.context.ConfigurableApplicationContext;<br><span class="hljs-keyword">import</span> org.springframework.stereotype.Component;<br><br><span class="hljs-meta">@Component</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyApplicationContextInitializer</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">ApplicationContextInitializer</span> &#123;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">initialize</span><span class="hljs-params">(ConfigurableApplicationContext applicationContext)</span> &#123;<br>        System.out.println(<span class="hljs-string">&quot;ApplicationContextInitializer....initialize&quot;</span>);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>



<p><code>MyApplicationRunner.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.uni.listener;<br><br><span class="hljs-keyword">import</span> org.springframework.boot.ApplicationArguments;<br><span class="hljs-keyword">import</span> org.springframework.boot.ApplicationRunner;<br><span class="hljs-keyword">import</span> org.springframework.stereotype.Component;<br><br><span class="hljs-keyword">import</span> java.util.Arrays;<br><br><span class="hljs-meta">@Component</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyApplicationRunner</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">ApplicationRunner</span> &#123;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">run</span><span class="hljs-params">(ApplicationArguments args)</span> <span class="hljs-keyword">throws</span> Exception &#123;<br>        System.out.println(<span class="hljs-string">&quot;ApplicationRunner...run&quot;</span>);<br>        System.out.println(Arrays.asList(args.getSourceArgs()));<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>



<p><code>MyCommandLineRunner.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.uni.listener;<br><br><span class="hljs-keyword">import</span> org.springframework.boot.CommandLineRunner;<br><span class="hljs-keyword">import</span> org.springframework.stereotype.Component;<br><br><span class="hljs-keyword">import</span> java.util.Arrays;<br><br><span class="hljs-meta">@Component</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyCommandLineRunner</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">CommandLineRunner</span> &#123;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">run</span><span class="hljs-params">(String... args)</span> <span class="hljs-keyword">throws</span> Exception &#123;<br>        System.out.println(<span class="hljs-string">&quot;CommandLineRunner...run&quot;</span>);<br>        System.out.println(Arrays.asList(args));<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>



<p><code>MySpringApplicationRunListener.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.uni.listener;<br><br><span class="hljs-keyword">import</span> org.springframework.boot.ConfigurableBootstrapContext;<br><span class="hljs-keyword">import</span> org.springframework.boot.SpringApplication;<br><span class="hljs-keyword">import</span> org.springframework.boot.SpringApplicationRunListener;<br><span class="hljs-keyword">import</span> org.springframework.context.ConfigurableApplicationContext;<br><span class="hljs-keyword">import</span> org.springframework.core.env.ConfigurableEnvironment;<br><span class="hljs-keyword">import</span> org.springframework.stereotype.Component;<br><br><span class="hljs-keyword">import</span> java.time.Duration;<br><br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">MySpringApplicationRunListener</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">SpringApplicationRunListener</span> &#123;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">MySpringApplicationRunListener</span><span class="hljs-params">(SpringApplication application, String[] args)</span> &#123;<br><br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">starting</span><span class="hljs-params">(ConfigurableBootstrapContext bootstrapContext)</span> &#123;<br>        System.out.println(<span class="hljs-string">&quot;starting...项目启动中&quot;</span>);<br><br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">environmentPrepared</span><span class="hljs-params">(ConfigurableBootstrapContext bootstrapContext, ConfigurableEnvironment environment)</span> &#123;<br>        System.out.println(<span class="hljs-string">&quot;environmentPrepared...环境对象开始准备&quot;</span>);<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">contextPrepared</span><span class="hljs-params">(ConfigurableApplicationContext context)</span> &#123;<br>        System.out.println(<span class="hljs-string">&quot;contextPrepared...上下文对象开始准备&quot;</span>);<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">contextLoaded</span><span class="hljs-params">(ConfigurableApplicationContext context)</span> &#123;<br>        System.out.println(<span class="hljs-string">&quot;contextLoaded...上下文对象开始加载&quot;</span>);<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">started</span><span class="hljs-params">(ConfigurableApplicationContext context, Duration timeTaken)</span> &#123;<br>        System.out.println(<span class="hljs-string">&quot;started...上下文对象加载完成&quot;</span>);<br>    &#125;<br><br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">failed</span><span class="hljs-params">(ConfigurableApplicationContext context, Throwable exception)</span> &#123;<br>        System.out.println(<span class="hljs-string">&quot;failed...项目启动失败&quot;</span>);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>运行结果：</p>
<p><img src="https://img-blog.csdnimg.cn/0c44d497c8eb454787dcec8e04640122.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h1 id="二、SpringBoot-原理分析"><a href="#二、SpringBoot-原理分析" class="headerlink" title="二、SpringBoot 原理分析"></a>二、SpringBoot 原理分析</h1><hr>
<p>图片来自视频资料：<a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1Lq4y1J77x?p=30">链接</a><br><img src="https://img-blog.csdnimg.cn/095b576b05e74cd79cd83978c0374ae0.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h2 id="3-1-init初始化"><a href="#3-1-init初始化" class="headerlink" title="3.1 init初始化"></a>3.1 init初始化</h2><p>在SpringBoot启动类的run方法处打一个断点进行调试</p>
<p><img src="https://img-blog.csdnimg.cn/925f4bcd374143a2b54f2cb06fd7e160.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>接下来进入到第一层，<code>SpringApplication.java</code> 执行了 run方法，不过是调用类本身的重载run方法</p>
<p><img src="https://img-blog.csdnimg.cn/ded6ad69c8eb465d8a4db7f95bbf7a70.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>另一个run方法又通过 <code>SpringApplication</code>类调用了run方法，注意这里传入了<code>primarySource</code>参数，即启动类的字节码</p>
<p><img src="https://img-blog.csdnimg.cn/91c2da7e9b6a43ddb0a4faf27e8b841a.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>接着查看类<code> SpringApplication.java</code>的构造方法，并在255行设置断点，接着恢复程序（F9）</p>
<p><img src="https://img-blog.csdnimg.cn/78ef4ddfc1f14a0dad820d9297e97679.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>逐行查看（F8），观察它的构造顺序<br><img src="https://img-blog.csdnimg.cn/69c91009159e498bb99619a81b884519.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>通过257行可以看到，SpringBoot启动类，被划分成数组后转化成了<code>LinkedHashSet</code>类型的哈希集合</p>
<p><img src="https://img-blog.csdnimg.cn/bfbcbee7b41c4edb8ab32bdbbaf8ea22.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>至此，SpringBoot项目启动的初始化部分了解完毕。</p>
<h2 id="3-2-run"><a href="#3-2-run" class="headerlink" title="3.2 run"></a>3.2 run</h2><p>和之前类似，这次在<code>SpringApplication.java</code>类的289行处打一个断点进行调试</p>
<p><img src="https://img-blog.csdnimg.cn/58f97577d9984fd9be4fa29e6af0748c.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>下图是上图293行的方法实现，这里是遍历了所有的启动依赖</p>
<p><img src="https://img-blog.csdnimg.cn/a2d70fcb2ed94e73bfad45f4a06c3959.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>这一步则是准备环境的方法实现，检查是否有配置的准备环境的监听类</p>
<p><img src="https://img-blog.csdnimg.cn/01526ec43215484d8142f73f61cccc10.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>准备环境之后，来到了Banner对象，这里就是打印SpringBoot图标的过程</p>
<p><img src="https://img-blog.csdnimg.cn/43c4be8cf2e94fd19d0382f32bb0312f.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>打印图标</p>
<p><img src="https://img-blog.csdnimg.cn/d44aef738c2041fa921993fc6e479c70.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>Banner是一个接口，通过查看其相关的类<code>SpringApplicationBannerPrinter.java</code>可以观察到一些默认的参数</p>
<p><img src="https://img-blog.csdnimg.cn/363b1ff79d224f6bbbb58b462a7202de.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>根据上图的参数，SpringBoot会扫描 <code>resources/banner.txt</code>里的内容作为启动信息，若没有则会默认输出Spring字样，现可以：</p>
<p>更换SpringBoot的启动提示</p>
<figure class="highlight gherkin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><code class="hljs gherkin">                   _ooOoo_  <br>                  o8888888o  <br>                  88<span class="hljs-string">&quot; . &quot;</span>88  <br>                  (|<span class="hljs-string"> -_- </span>|<span class="hljs-string">)  </span><br><span class="hljs-string">                   O\ = /O  </span><br><span class="hljs-string">               ____/`---&#x27;\____  </span><br><span class="hljs-string">             .   &#x27; \\</span>|<span class="hljs-string"> </span>|<span class="hljs-string">// `.  </span><br><span class="hljs-string">              / \\</span>|||<span class="hljs-string"> : </span>|||<span class="hljs-string">// \  </span><br><span class="hljs-string">            / _</span>|||||<span class="hljs-string"> -:- </span>|||||<span class="hljs-string">- \  </span><br><span class="hljs-string">              </span>|<span class="hljs-string"> </span>|<span class="hljs-string"> \\\ - /// </span>|<span class="hljs-string"> </span>|<span class="hljs-string">  </span><br><span class="hljs-string">            </span>|<span class="hljs-string"> \_</span>|<span class="hljs-string"> &#x27;&#x27;\---/&#x27;&#x27; </span>|<span class="hljs-string"> </span>|<span class="hljs-string">  </span><br><span class="hljs-string">             \ .-\__ `-` ___/-. /  </span><br><span class="hljs-string">          ___`. .&#x27; /--.--\ `. . __  </span><br><span class="hljs-string">       .&quot;&quot; &#x27;&lt; `.___\_&lt;</span>|<span class="hljs-string">&gt;_/___.&#x27; &gt;&#x27;&quot;&quot;.  </span><br><span class="hljs-string">      </span>|<span class="hljs-string"> </span>|<span class="hljs-string"> : `- \`.;`\ _ /`;.`/ - ` : </span>|<span class="hljs-string"> </span>|<span class="hljs-string">  </span><br><span class="hljs-string">        \ \ `-. \_ __\ /__ _/ .-` / /  </span><br><span class="hljs-string">======`-.____`-.___\_____/___.-`____.-&#x27;======  </span><br><span class="hljs-string">                   `=---=&#x27;  </span><br><span class="hljs-string"></span><br><span class="hljs-string">.............................................  </span><br><span class="hljs-string">         佛祖保佑             永无BUG </span><br></code></pre></td></tr></table></figure>

<p>将文本的内容放在resources &#x2F; banner.txt里，没有则创建，然后启动项目，替换成功~</p>
<p><img src="https://img-blog.csdnimg.cn/b97d5c0f965f4aec8b76019868610177.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>Spring启动源码</p>
<p><code>SpringApplication.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> ConfigurableApplicationContext <span class="hljs-title function_">run</span><span class="hljs-params">(String... args)</span> &#123;<br>    <span class="hljs-comment">/** 计时 **/</span><br>    <span class="hljs-type">long</span> <span class="hljs-variable">startTime</span> <span class="hljs-operator">=</span> System.nanoTime();<br>    <span class="hljs-type">DefaultBootstrapContext</span> <span class="hljs-variable">bootstrapContext</span> <span class="hljs-operator">=</span> createBootstrapContext();<br>    <span class="hljs-type">ConfigurableApplicationContext</span> <span class="hljs-variable">context</span> <span class="hljs-operator">=</span> <span class="hljs-literal">null</span>;<br>    configureHeadlessProperty();<br>    <span class="hljs-type">SpringApplicationRunListeners</span> <span class="hljs-variable">listeners</span> <span class="hljs-operator">=</span> getRunListeners(args);<br>    <span class="hljs-comment">/*** ***/</span> <br>    listeners.starting(bootstrapContext, <span class="hljs-built_in">this</span>.mainApplicationClass);<br>    <span class="hljs-keyword">try</span> &#123;<br>        <span class="hljs-type">ApplicationArguments</span> <span class="hljs-variable">applicationArguments</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">DefaultApplicationArguments</span>(args);<br>        <span class="hljs-type">ConfigurableEnvironment</span> <span class="hljs-variable">environment</span> <span class="hljs-operator">=</span> prepareEnvironment(listeners, bootstrapContext, applicationArguments);<br>        configureIgnoreBeanInfo(environment);<br>        <span class="hljs-type">Banner</span> <span class="hljs-variable">printedBanner</span> <span class="hljs-operator">=</span> printBanner(environment);<br>        context = createApplicationContext();<br>        context.setApplicationStartup(<span class="hljs-built_in">this</span>.applicationStartup);<br>        prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);<br>        refreshContext(context);<br>        afterRefresh(context, applicationArguments);<br>        <span class="hljs-type">Duration</span> <span class="hljs-variable">timeTakenToStartup</span> <span class="hljs-operator">=</span> Duration.ofNanos(System.nanoTime() - startTime);<br>        <span class="hljs-keyword">if</span> (<span class="hljs-built_in">this</span>.logStartupInfo) &#123;<br>            <span class="hljs-keyword">new</span> <span class="hljs-title class_">StartupInfoLogger</span>(<span class="hljs-built_in">this</span>.mainApplicationClass).logStarted(getApplicationLog(), timeTakenToStartup);<br>        &#125;<br>        listeners.started(context, timeTakenToStartup);<br>        callRunners(context, applicationArguments);<br>    &#125;<br>    <span class="hljs-keyword">catch</span> (Throwable ex) &#123;<br>        handleRunFailure(context, ex, listeners);<br>        <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">IllegalStateException</span>(ex);<br>    &#125;<br>    <span class="hljs-keyword">try</span> &#123;<br>        <span class="hljs-type">Duration</span> <span class="hljs-variable">timeTakenToReady</span> <span class="hljs-operator">=</span> Duration.ofNanos(System.nanoTime() - startTime);<br>        listeners.ready(context, timeTakenToReady);<br>    &#125;<br>    <span class="hljs-keyword">catch</span> (Throwable ex) &#123;<br>        handleRunFailure(context, ex, <span class="hljs-literal">null</span>);<br>        <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">IllegalStateException</span>(ex);<br>    &#125;<br>    <span class="hljs-keyword">return</span> context;<br>&#125;<br></code></pre></td></tr></table></figure>



<h1 id="三、-SpringBoot-监控概述"><a href="#三、-SpringBoot-监控概述" class="headerlink" title="三、 SpringBoot 监控概述"></a>三、 SpringBoot 监控概述</h1><hr>
<p>SpringBoot 自带监控功能 Actuator， 一帮助实现对程序内部运行情况监控，比如监控状况，Bean加载情况、配置属性、日志信息等。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         </p>
<p><strong>使用步骤</strong>：导入起步依赖坐标即可</p>
<p><code>pom.xml</code></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework.boot<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-boot-starter-actuator<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p>访问：<a target="_blank" rel="noopener" href="http://localhost:8080/acruator">http://localhost:8080/acruator</a></p>
<p><img src="https://img-blog.csdnimg.cn/ec6af53be8e7473c813d72b24f382ca5.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>返回结果是一个JSON格式的数据，health表示SpringBoot项目里的application所有health字段的内容</p>
<p>接着手动打开配置项目的health监控</p>
<p><code>application.properties</code></p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs properties"><span class="hljs-comment"># 开启健康检查的完整信息</span><br><span class="hljs-attr">management.endpoint.health.show-details</span>=<span class="hljs-string">always</span><br></code></pre></td></tr></table></figure>

<p>启动项目后访问：<a target="_blank" rel="noopener" href="http://localhost:8080/acruator/health">http://localhost:8080/acruator/health</a></p>
<p><img src="https://img-blog.csdnimg.cn/e157c9cf5aac4ce383c39f9e6d6d3005.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>现在添加Redis依赖，同时本机不启动Redis，再查看监控的内容</p>
<p><code>pom.xml</code></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework.boot<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-boot-starter-data-redis<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p>启动项目，访问 <a target="_blank" rel="noopener" href="http://localhost:8080/actuator/health">http://localhost:8080/actuator/health</a>，将结果在 <a target="_blank" rel="noopener" href="https://www.json.cn/">https://www.json.cn</a> 里格式化后：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br>    <span class="hljs-attr">&quot;status&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-string">&quot;DOWN&quot;</span><span class="hljs-punctuation">,</span><br>    <span class="hljs-attr">&quot;components&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-punctuation">&#123;</span><br>        <span class="hljs-attr">&quot;diskSpace&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-punctuation">&#123;</span><br>            <span class="hljs-attr">&quot;status&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-string">&quot;UP&quot;</span><span class="hljs-punctuation">,</span><br>            <span class="hljs-attr">&quot;details&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-punctuation">&#123;</span><br>                <span class="hljs-attr">&quot;total&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-number">296022437888</span><span class="hljs-punctuation">,</span><br>                <span class="hljs-attr">&quot;free&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-number">128095268864</span><span class="hljs-punctuation">,</span><br>                <span class="hljs-attr">&quot;threshold&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-number">10485760</span><span class="hljs-punctuation">,</span><br>                <span class="hljs-attr">&quot;exists&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-literal"><span class="hljs-keyword">true</span></span><br>            <span class="hljs-punctuation">&#125;</span><br>        <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>        <span class="hljs-attr">&quot;ping&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-punctuation">&#123;</span><br>            <span class="hljs-attr">&quot;status&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-string">&quot;UP&quot;</span><br>        <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span><br>        <span class="hljs-attr">&quot;redis&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-punctuation">&#123;</span><br>            <span class="hljs-attr">&quot;status&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-string">&quot;DOWN&quot;</span><span class="hljs-punctuation">,</span><br>            <span class="hljs-attr">&quot;details&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-punctuation">&#123;</span><br>                <span class="hljs-attr">&quot;error&quot;</span><span class="hljs-punctuation">:</span><span class="hljs-string">&quot;org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to localhost:6379&quot;</span><br>            <span class="hljs-punctuation">&#125;</span><br>        <span class="hljs-punctuation">&#125;</span><br>    <span class="hljs-punctuation">&#125;</span><br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>

<p>可以观察到，导入Redis起步依赖后，SpringBoot能自动监听Redis的启动情况。</p>
<p>显示所有的监控 endpoint</p>
<p><code>application.properties</code></p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs properties"><span class="hljs-comment"># 显示所有的监控 endpoint</span><br><span class="hljs-attr">management.endpoints.web.exposure.include</span>=<span class="hljs-string">*</span><br></code></pre></td></tr></table></figure>

<p>启动项目，访问 <a target="_blank" rel="noopener" href="http://localhost:8080/actuator">http://localhost:8080/actuator</a></p>
<p><img src="https://img-blog.csdnimg.cn/1fee780e5783468aa18cf4f2fb41c47e.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>网址里提供了许多其他的监控地址，这里查看一下bean的监控地址：<a target="_blank" rel="noopener" href="http://localhost:8080/actuator/beans">http://localhost:8080/actuator/beans</a></p>
<p><img src="https://img-blog.csdnimg.cn/1486f833a53c4a62a36aa1402258c640.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p><a target="_blank" rel="noopener" href="http://localhost:8080/actuator/mappings">http://localhost:8080/actuator/mappings </a>显示监控SpringBoot项目中类的调用信息</p>
<h2 id="3-1-SpringBoot-监控-Spring-Boot-Admin"><a href="#3-1-SpringBoot-监控-Spring-Boot-Admin" class="headerlink" title="3.1 SpringBoot 监控 - Spring Boot Admin"></a>3.1 SpringBoot 监控 - Spring Boot Admin</h2><ul>
<li>Spring Boot Admin 是一个开源社区项目，用于管理和监控 SpringBoot 应用程序</li>
<li>Spring Boot Admin 有两个角色，客户端（Client）和服务端（Server）</li>
<li>应用程序作为Spring Boot Admin Client 向为 Spring Boot Admin Server 注册</li>
<li>Spring Boot Admin Server 的UI界面将 Spring Boot Admin Client 的 Actuator Endpoint 上的一些监控信息</li>
</ul>
<h2 id="3-2-使用-Admin-案例"><a href="#3-2-使用-Admin-案例" class="headerlink" title="3.2 使用 Admin 案例"></a>3.2 使用 Admin 案例</h2><p>使用步骤：</p>
<p><strong>admin-server</strong>:</p>
<ol>
<li>创建 admin-server 模块</li>
<li>导入依赖坐标 admin-starter-server</li>
</ol>
<p><code>pom.xml</code>（部分）</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">properties</span>&gt;</span><br>   <span class="hljs-tag">&lt;<span class="hljs-name">java.version</span>&gt;</span>1.8<span class="hljs-tag">&lt;/<span class="hljs-name">java.version</span>&gt;</span><br>   <span class="hljs-tag">&lt;<span class="hljs-name">spring-boot-admin.version</span>&gt;</span>2.6.0<span class="hljs-tag">&lt;/<span class="hljs-name">spring-boot-admin.version</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">properties</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">dependencies</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework.boot<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-boot-starter-web<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>de.codecentric<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-boot-admin-starter-server<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework.boot<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-boot-starter-test<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">scope</span>&gt;</span>test<span class="hljs-tag">&lt;/<span class="hljs-name">scope</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependencies</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">dependencyManagement</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependencies</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>de.codecentric<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-boot-admin-dependencies<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>$&#123;spring-boot-admin.version&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">type</span>&gt;</span>pom<span class="hljs-tag">&lt;/<span class="hljs-name">type</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">scope</span>&gt;</span>import<span class="hljs-tag">&lt;/<span class="hljs-name">scope</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependencies</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependencyManagement</span>&gt;</span><br></code></pre></td></tr></table></figure>

<ol start="3">
<li>在引导类上启动监控功能@EnableAdminServer<br>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/6b48c5ba8ab9487783cee05a6f3e3d54.png#pic_center">https://img-blog.csdnimg.cn/6b48c5ba8ab9487783cee05a6f3e3d54.png#pic_center</a> &#x3D;80%x)</li>
</ol>
<p><strong>admin-client:</strong></p>
<ol>
<li>创建 admin-client 模块</li>
<li>导入依赖坐标 admin-starter-client</li>
</ol>
<p><code>pom.xml</code></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">properties</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">java.version</span>&gt;</span>1.8<span class="hljs-tag">&lt;/<span class="hljs-name">java.version</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">spring-boot-admin.version</span>&gt;</span>2.6.0<span class="hljs-tag">&lt;/<span class="hljs-name">spring-boot-admin.version</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">properties</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">dependencies</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework.boot<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-boot-starter-web<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>de.codecentric<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-boot-admin-starter-client<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework.boot<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-boot-starter-test<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">scope</span>&gt;</span>test<span class="hljs-tag">&lt;/<span class="hljs-name">scope</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependencies</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">dependencyManagement</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependencies</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>de.codecentric<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-boot-admin-dependencies<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>$&#123;spring-boot-admin.version&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">type</span>&gt;</span>pom<span class="hljs-tag">&lt;/<span class="hljs-name">type</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">scope</span>&gt;</span>import<span class="hljs-tag">&lt;/<span class="hljs-name">scope</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependencies</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependencyManagement</span>&gt;</span><br><br></code></pre></td></tr></table></figure>

<ol start="3">
<li>配置相关信息：server地址等</li>
</ol>
<p>服务端配置 <code>application.properties</code></p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs properties"><span class="hljs-attr">server.port</span>=<span class="hljs-string">9000</span><br></code></pre></td></tr></table></figure>
<p>客户端配置 <code>application.properties</code></p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs properties"><span class="hljs-comment"># 执行 admin.server 地址</span><br><span class="hljs-attr">spring.boot.admin.client.url</span>=<span class="hljs-string">http://localhost:9000</span><br><br><span class="hljs-attr">management.endpoint.health.show-details</span>=<span class="hljs-string">always</span><br><span class="hljs-attr">management.endpoints.web.exposure.include</span>=<span class="hljs-string">*</span><br></code></pre></td></tr></table></figure>
<ol start="4">
<li>启动server和client服务，访问server</li>
</ol>
<p><img src="https://img-blog.csdnimg.cn/7092eeb4f1444a7ab0f5b168b1a33e98.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h2 id="3-3-查看监控信息"><a href="#3-3-查看监控信息" class="headerlink" title="3.3 查看监控信息"></a>3.3 查看监控信息</h2><p>访问：<a target="_blank" rel="noopener" href="http://localhost:9000/applications">http://localhost:9000/applications</a>，查看监控信息</p>
<p><img src="https://img-blog.csdnimg.cn/a48a9ec11df94ea6b39ed5201736699d.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h2 id="3-4-查看访问次数"><a href="#3-4-查看访问次数" class="headerlink" title="3.4 查看访问次数"></a>3.4 查看访问次数</h2><p><code>UserController.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.uni;<br><br><span class="hljs-keyword">import</span> org.springframework.web.bind.annotation.RequestMapping;<br><span class="hljs-keyword">import</span> org.springframework.web.bind.annotation.RestController;<br><br><br><span class="hljs-meta">@RestController</span><br><span class="hljs-meta">@RequestMapping(&quot;/user&quot;)</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">UserController</span> &#123;<br>    <span class="hljs-meta">@RequestMapping(&quot;/findAll&quot;)</span><br>    <span class="hljs-keyword">public</span> String <span class="hljs-title function_">findAll</span><span class="hljs-params">()</span> &#123; <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;success&quot;</span>; &#125;<br>&#125;<br><br></code></pre></td></tr></table></figure>
<p>访问客户端访问三次：<a target="_blank" rel="noopener" href="http://localhost:8080/user/findAll">http://localhost:8080/user/findAll
</a></p>
<p>访问服务端，并查看请求结果</p>
<p><img src="https://img-blog.csdnimg.cn/c78ddbf9c0674e42b6abb5983ab5546a.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h2 id="3-5-查看注入Spring容器的Bean"><a href="#3-5-查看注入Spring容器的Bean" class="headerlink" title="3.5 查看注入Spring容器的Bean"></a>3.5 查看注入Spring容器的Bean</h2><p><img src="https://img-blog.csdnimg.cn/85f49902b31841578f02629e3f004216.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h2 id="3-6-查看部署项目线程实时的运行情况"><a href="#3-6-查看部署项目线程实时的运行情况" class="headerlink" title="3.6 查看部署项目线程实时的运行情况"></a>3.6 查看部署项目线程实时的运行情况</h2><p><img src="https://img-blog.csdnimg.cn/9aee01a8bb0843df8d328069f5a7ae6b.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h2 id="3-7-IDEA-提供的监控可视化"><a href="#3-7-IDEA-提供的监控可视化" class="headerlink" title="3.7 IDEA 提供的监控可视化"></a>3.7 IDEA 提供的监控可视化</h2><p><img src="https://img-blog.csdnimg.cn/6da37d6756604ac4a1d8ea58b6883775.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h1 id="四、SpringBoot-项目部署"><a href="#四、SpringBoot-项目部署" class="headerlink" title="四、SpringBoot 项目部署"></a>四、SpringBoot 项目部署</h1><hr>
<p>SpringBoot项目开发完毕后，支持两种方式部署到服务器：</p>
<ul>
<li>jar包（官方推荐）</li>
<li>war包</li>
</ul>
<h2 id="4-1-Jar"><a href="#4-1-Jar" class="headerlink" title="4.1 Jar"></a>4.1 Jar</h2><p>默认模式，创建SpringBoot项目后打包默认为Jar包</p>
<p>在IDEA中通过Maven插件进行打包</p>
<p><img src="https://img-blog.csdnimg.cn/ad4fa88b0f6c4a97bd51ede61b26475c.png#pic_Center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>然后在终端运行jar包</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell">java -jar *.jar<br></code></pre></td></tr></table></figure>

<p><img src="https://img-blog.csdnimg.cn/54f5fca77b6442fb8de578fa94a0e765.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h2 id="4-2-war"><a href="#4-2-war" class="headerlink" title="4.2 war"></a>4.2 war</h2><p><code>pom.xml</code></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">packaging</span>&gt;</span>war<span class="hljs-tag">&lt;/<span class="hljs-name">packaging</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p>SpringBoot启动类需要继承 <code>SpringBootServletInitializer类</code>并重写一个配置的方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.uni;<br><br><span class="hljs-keyword">import</span> org.springframework.boot.SpringApplication;<br><span class="hljs-keyword">import</span> org.springframework.boot.autoconfigure.SpringBootApplication;<br><span class="hljs-keyword">import</span> org.springframework.boot.builder.SpringApplicationBuilder;<br><span class="hljs-keyword">import</span> org.springframework.boot.web.servlet.support.SpringBootServletInitializer;<br><br><span class="hljs-meta">@SpringBootApplication</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">SpringbootDeployApplication</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">SpringBootServletInitializer</span> &#123;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>        SpringApplication.run(SpringbootDeployApplication.class, args);<br>    &#125;<br><br>    <span class="hljs-comment">/** War包 **/</span><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">protected</span> SpringApplicationBuilder <span class="hljs-title function_">configure</span><span class="hljs-params">(SpringApplicationBuilder builder)</span> &#123;<br>        <span class="hljs-keyword">return</span> builder.sources(SpringbootDeployApplication.class);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>然后使用 Maven打包成war包</p>
<p><img src="https://img-blog.csdnimg.cn/e5c02e81be144e44a0ba3cb0da59ca0d.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>若要指定包名则在 <code>pom.xml </code> 里的<code>&lt;build&gt;</code>标签里加上<code>&lt;finalName&gt;包名&lt;/finalName&gt;</code>即可</p>
<p>将war包配置到外部的Tomcat</p>
<p><img src="https://img-blog.csdnimg.cn/c693d37fb9d3453686432e866bfa367e.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>启动 Tomcat</p>
<p><img src="https://img-blog.csdnimg.cn/359a12610f4144eda125d364e716b54e.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>访问网址：<a target="_blank" rel="noopener" href="http://localhost:8080/springboot">http://localhost:8080/springboot</a> </p>
<h1 id="五、收获与总结"><a href="#五、收获与总结" class="headerlink" title="五、收获与总结"></a>五、收获与总结</h1><hr>
<p>通过本次的学习，了解到了SpringBoot的四个监听机制，同时跟着视频资料对SpringBoot项目启动的过程进行了简单的调试，通过打断点来观察其运行的原理，调用了哪些类，然后调用了哪些方法，方法的作用等等。</p>
<p>第一次了解到SpringBoot的监听机制，这使笔者想到上学期刚学过的web里面servlet的监听器listener，可以监听servlet或者变量之类的。</p>
<p>通过这几天的学习，现在可以简单的使用SpringBoot进行开发，比如写一个web后端页面，使用全注解开发，然后配置的话只需要在application前缀的配置文件里进行修改，springboot的起步依赖机制真的太方便了，希望在以后的学习与实践中能够熟练运用。</p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/SpringBoot/" class="category-chain-item">SpringBoot</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/SpringBoot/">#SpringBoot</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>SpringBoot 笔记4 SpringBoot原理分析 _ 四个监听机制 _ 通过断点调试进行原理分析 _ Admin监控机制的运用 _ SpringBoot部署模式切换jar与war</div>
      <div>http://example.com/2022/01/12/SpringBoot 笔记4 SpringBoot原理分析 _ 四个监听机制 _ 通过断点调试进行原理分析 _ Admin监控机制的运用 _ SpringBoot部署模式切换jar与war/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>John Doe</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年1月12日</div>
        </div>
      
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/01/12/Web%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98%20_%20%E5%95%86%E5%9F%8E%E7%B3%BB%E7%BB%9Fv1.0%20_%20%E5%89%8D%E7%AB%AFJSP%20+%20Servlet%20+%20Bootstrap5%20+%20JQuery%20+%20Echarts%20_%20%E5%90%8E%E7%AB%AF%20MVC%E6%9E%B6%E6%9E%84%20+%20JDBC%20+%20Druid%E6%95%B0%E6%8D%AE%E6%B1%A0/" title="Web项目实战 _ 商城系统v1.0 _ 前端JSP + Servlet + Bootstrap5 + JQuery + Echarts _ 后端 MVC架构 + JDBC + Druid数据池">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Web项目实战 _ 商城系统v1.0 _ 前端JSP + Servlet + Bootstrap5 + JQuery + Echarts _ 后端 MVC架构 + JDBC + Druid数据池</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/01/08/SpringBoot%20%E7%AC%94%E8%AE%B03%20SpringBoot%20%E8%87%AA%E5%8A%A8%E9%85%8D%E7%BD%AE%20_%20Condition%E7%9A%84%E5%AE%9E%E7%8E%B0%20_%20%E5%88%87%E6%8D%A2web%E5%86%85%E7%BD%AE%E6%9C%8D%E5%8A%A1%E5%99%A8%20_%20@Enable%20%E6%B3%A8%E8%A7%A3%20_%20@Import%E6%B3%A8%E8%A7%A3%20_%E8%87%AA%E5%8A%A8%E9%85%8D%E7%BD%AE%E6%A8%A1%E6%8B%9F%E5%AE%9E%E7%8E%B0/" title="SpringBoot 笔记3 SpringBoot 自动配置 _ Condition的实现 _ 切换web内置服务器 _ @Enable 注解 _ @Import注解 _自动配置模拟实现">
                        <span class="hidden-mobile">SpringBoot 笔记3 SpringBoot 自动配置 _ Condition的实现 _ 切换web内置服务器 _ @Enable 注解 _ @Import注解 _自动配置模拟实现</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
